package com.bfg.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class QuestionRequestServlet extends HttpServlet {
	private TestDBConnectionManager mgr;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		mgr = TestDBConnectionManager.getInstance();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		
		HttpSession session = req.getSession();
//		session.removeAttribute("entryError");

		String action = req.getParameter("action");
		
		Connection conn = mgr.getConnection("mssql");
		if (conn == null) {
			out.println("can't get connection");
			return;
		}
		PreparedStatement prestmt;
		
		if (action.equals("insert1")) {
			System.out.println("in");
			String inPhoneNumber=validatePhoneNumber(req.getParameter("txtTitle1"));
			System.out.println(inPhoneNumber);
			try {
				prestmt = conn
						.prepareStatement("INSERT INTO SMS_SEND_QUEUE(division, msisdn, contents, replyto, status, input_date) VALUES (?,?,?,?,?,getdate())");
//				prestmt.setInt(1, 1);
				prestmt.setString(1, "W");
				prestmt.setString(2, inPhoneNumber);
				prestmt.setString(3, "$$+$$");
				prestmt.setString(4, "628998333833");
				prestmt.setString(5, "0");
				
				if (prestmt.executeUpdate() == 1) {
					prestmt.close();
//					out.println("data has been inserted");
//					session.setAttribute("entry1Error", "n");
//					goToPage("/customer/customer_requested.html", req, res);
					res.sendRedirect("http://localhost:8080/bizring/jsp/customer_requested.jsp");
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if (action.equals("insert2")) {
			System.out.println("in2");
			String inPhoneNumber=validatePhoneNumber(req.getParameter("txtTitle2"));
			System.out.println(inPhoneNumber);
			try {
				prestmt = conn
						.prepareStatement("INSERT INTO SMS_SEND_QUEUE(division, msisdn, contents, replyto, status, input_date) VALUES (?,?,?,?,?,getdate())");
//				prestmt.setInt(1, 1);
				prestmt.setString(1, "W");
				prestmt.setString(2, inPhoneNumber);
				prestmt.setString(3, "$$-$$");
				prestmt.setString(4, "628998333833");
				prestmt.setString(5, "0");
				
				if (prestmt.executeUpdate() == 1) {
					prestmt.close();
//					out.println("data has been inserted");
//					session.setAttribute("entry2Error", "n");
//					goToPage("/customer/customer_requested.html", req, res);
					res.sendRedirect("http://localhost:8080/bizring/jsp/customer_requested.jsp");
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		mgr.freeConnection("mssql", conn);
	}

	@Override
	public void destroy() {
		mgr.release();
		super.destroy();
	}
	
	// sending request to different page
	private void goToPage(String page, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		RequestDispatcher rd = req.getRequestDispatcher(page);
		if (rd != null)
			rd.forward(req, resp);

	}

	private String validatePhoneNumber(String strPhoneNumber) {
		String strCode="62";
		String strPhone="";
		if(strPhoneNumber.charAt(0)=='0'){
			System.out.println("0");
			strPhoneNumber=strPhoneNumber.substring(1, strPhoneNumber.length());
		}
		System.out.println(strPhoneNumber);
		return strCode+strPhoneNumber;
		
	}
}
